package com.cloudpark.common.util;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

/**
 * 日期处理工具类
 */
public class DateUtils {

    public static String YMD2 = "yyyyMMdd";
    public static String YMD = "yyyy-MM-dd";
    public static String Y = "yyyy";
    public static String M = "MM";
    public static String D = "dd";
    public static String YM = "yyyy-MM";
    public static String HHMMSS = "HH:mm:ss";
    public static String hhMM = "hh:mm";
    public static String HHMM = "HH:mm";
    public static String YMDHMS = "yyyy-MM-dd HH:mm:ss";
    public static String YMDHMS2 = "yyyyMMddHHmmss";

    public static String HH = "HH";
    public static String MM = "mm";
    public static String SS = "ss";

    /**
     * 字符串转日期
     */
    public static Date toDate(String str, String pattern) {
        if (StringUtils.isEmpty(pattern)) {
            throw new IllegalArgumentException("日期格式不能为空!");
        }
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat(pattern);
        try {
            return sf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 日期转字符串
     */
    public static String toString(Date date, String pattern) {
        if (StringUtils.isEmpty(pattern)) {
            throw new IllegalArgumentException("日期格式不能为空!");
        }
        java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat(pattern);
        return sf.format(date);
    }
}